<template>
	<div @touchmove.stop.prevent="moveHandle">
		<u-popup :show="show" mode="center" zIndex='99'>
			<view class="popup-view">
				<view class="view-title">
					请选择
				</view>
				<view class="view-conster">
					<view class="constant-top">
						<view class="constant-heard">
							选择项 ↓
						</view>
						<view class="top-top">
							<scroll-view scroll-y="true" style="height:100%;width:100%;">
								<view v-for="(item,index) in bindNO" :key="index" class="top-conter"
									@tap="CheckFun(item,index)">
									<view class="conter-right">
										<view class="top-title">
											<text style="width: 20%;">
												{{item.dosageday}}
											</text>
											<text style="width: 65%;">
												{{item.ordername}}
											</text>
											<text style="width: 15%;">
												{{item.orderflag}}
											</text>
										</view>
									</view>
								</view>
							</scroll-view>
						</view>
					</view>
					<view class="constant-top">
						<view class="constant-heard">
							已选项 ↓
						</view>
						<view class="top-top">
							<scroll-view scroll-y="true" style="height:100%;width:100%;">
								<view v-for="(item,index) in dataYes" :key="index" class="top-conter"
									@tap="CheckDataYes(item,index)">
									<view class="conter-right">
										<view class="top-title">
											<view style="width: 25%;display: flex; align-items: center;"
												@tap.stop="clickname1">
												<u-input v-model="item.dosageday" type="tel" pattern="[0-9]*"
													@tap.stop="clickname(item.dosageday)"
													:customStyle="{border: '1px solid #dadbde !important'}"
													adjustPosition=""></u-input>
												<text style="margin-left: 10px;">{{item.unit}}</text>
											</view>
											<text style="width: 65%;">
												{{item.ordername}}
											</text>
											<text style="width: 15%;">
												{{item.orderflag}}
											</text>
										</view>
									</view>
								</view>
							</scroll-view>
						</view>
					</view>
				</view>
				<view class="btn">
					<u-button text="确认" type="primary" color="#82ECC3" @tap="cancel"></u-button>
					<u-button text="还原" type="primary" color="#0055ff" style="margin: 0 5px;" @tap="reduction">
					</u-button>
					<u-button text="取消" type="primary" color="#FF6E91" @tap="radioChange"></u-button>
				</view>
			</view>
		</u-popup>
	</div>
</template>

<script>
	import {
		GetDataBind,
		GetSelectDataBind,
		initDataRowDT,
		isChargeMrnDT
	} from "@/api/user.js"
	import {
		mapMutations,
		mapState
	} from "vuex";
	export default {
		props: {
			show: {
				type: Boolean,
				default: false
			},
			reportObj: {
				type: Object
			}
		},

		data() {
			return {
				list: [],
				bindNO: [],
				dataYes: [],
				SelectedList: []
			}
		},
		computed: {
			...mapState('user', ["userInfo", "deleteObj", "scanningArray"]),
		},
		mounted() {
			this.list = this.reportObj.children
			if (this.list?.length > 0) {
				this.GetDataBindFun()
				this.GetSelectDataBindFun()
			}
		},
		methods: {
			cancel() {
				if (this.dataYes.length <= 0) {
					return
				} else {
					let iReturn = ";";
					let iReShow = false
					this.dataYes.map((item, index) => {
						if (item.dosageday == '' || item.dosageday < 0) {
							uni.showToast({
								title: `第${parseInt(index + 1)}行，不能输入小于0的值`, //显示的文字
								duration: 2000, //显示多少时间，默认1500毫秒
								icon: "none", //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
								mask: true
							})
							iReShow = true
							return
						}
						iReturn = iReturn + item.drcode.trim() + ',' + item.dosageday + ';'
					})
					if (!iReShow) {
						console.log(iReturn, 'iReturn');
						this.$emit('bulletTion', false, this.reportObj.children, 'queren', iReturn);
					}
				}
			},
			StopImplement() {

			},
			clickname(e) {
				console.log('111', e);
			},
			clickname1(e) {
				// console.log('111',e);
			},
			radioChange(data) {
				
				uni.showModal({
					title: '提示',
					content: '是否取消收费用药?',
					success:  (res)=> {
						if (res.confirm) {
						this.$emit('bulletTion', false, this.reportObj.children, 'quxiao');
						this.GetSelectDataBindFun()
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				
			
			},
			reduction() {
				this.list = this.reportObj.children;
				this.GetSelectDataBindFun()
			},
			async CheckFun(data, index) {
				let flag = false
				this.dataYes.map(item => {
					if (item.drcode == data.drcode) {
						flag = true
						return
					}
				})
				if (data.unit == '小时') {
					const res = await this.initDataRowDTFun(data)
					this.dataYes.push(res)
				} else {
					if (!flag) {
						const res = await this.isChargeMrnDTFun(data)
						if (res.result == '1') {
							// // const resList = await this.initDataRowDTFun(data)
							// console.log(data, 'obj');
							// data.dosageday = ''
							this.dataYes.push({
								...data,
								dosageday: ''
							})
							console.log(this.dataYes, 'this.dataYes');
						}

					}
				}
			},
			CheckDataYes(data, index) {
				if (this.dataYes.length > 0) {
					this.dataYes.map((item, index) => {
						if (item.drcode == data.drcode) {
							this.dataYes.splice(index, 1)
						}
					})
				}
			},
			async initDataRowDTFun(obj) {
				const user = uni.getStorageSync('userInfo')
				const data = {
					HosKind: user.hoscode,
					dosageday: obj.dosageday,
					drcode: obj.drcode,
					ordername: obj.ordername,
					orderflag: obj.orderflag,
					onlyflag: obj.onlyflag,
					unit: obj.unit,
					_mrn: this.list[0].mrn,
					_series: this.list[0].series,
					_rdn: this.list[0].mainrdn,
					_orderno: this.list[0].orderno
				}
				console.log(data);
				const res = await this.$http.initDataRowDT(data);
				if (res[0].result) {
					return res[0]
				}
				// console.log(res);

			},
			async isChargeMrnDTFun(obj) {
				const user = uni.getStorageSync('userInfo')
				const data = {
					HosKind: user.hoscode,
					onlyflag: obj.onlyflag,
					orderflag: obj.orderflag,
					drcode: obj.drcode,
					mrn: this.list[0].mrn,
					series: this.list[0].series,
				}
				const res = await this.$http.isChargeMrnDT(data)
				if (res[0].result == '1') {
					return res[0]
				}
			},
			async GetDataBindFun() {
				const user = uni.getStorageSync('userInfo')
				const data = {
					HosKind: user.hoscode,
					docode: this.list[0].docode
				}
				const res = await this.$http.GetDataBind(data);
				for (let i = 0; i < res.length; i++) {
					if (res[i].result == '1') {
						this.bindNO.push(res[i])
					} else {
						this.bindNO.push(res[i].result)
					}
				}
			},
			async GetSelectDataBindFun() {
				const user = uni.getStorageSync('userInfo')
				const data = {
					HosKind: user.hoscode,
					docode: this.list[0].docode,
					_mrn: this.list[0].mrn,
					_series: this.list[0].series,
					rdn: this.list[0].mainrdn,
					_orderno: this.list[0].orderno
				}
				  this.dataYes =  []
				const res = await this.$http.GetSelectDataBind(data)
				for (let i = 0; i < res.length; i++) {
					if (res[i].result == '1') {
						this.dataYes.push(res[i])
					}else{
						this.dataYes.push(res[i])
					}
				}
    
				// let dataYes
				if (this.dataYes == null || this.dataYes.length <= 0) {
					this.dataYes = []
				}
			},
			checkRepeat(arr, value) {
				var array = [];
				arr.forEach((item) => {
					if (item) {
						array.push(item[value]);
					}
				});
				if ((new Set(array)).size != arr.length) {
					return arr;
				}
				return false;
			},
			moveHandle() {}
		}
	}
</script>


<style scoped lang="scss">
	::v-deep .u-popup {
		.u-fade-enter-active {
			z-index: 99 !important;
		}
	}

	::v-deep .u-popup__content {
		width: 100%;
		height: 100vh;
	}

	.popup-view {
		// width: 670rpx;
		height: 460px;
		// background: #FFFFFF;
		border-radius: 8rpx;
		position: relative;
		// padding-bottom: 15rpx;

		.view-title {
			background-color: #5555ff;
			color: #fff;
			font-size: 18px;
			line-height: 36px;
			padding-left: 10px;
			padding-top: 17px;
		}

		.view-conster {
			height: 100%;

			.constant-top,
			.constant-bottom {
				height: 50%;

				.constant-heard {
					background-color: #3e5bab;
					color: #fff;
					font-size: 16px;
					line-height: 32px;
				}

				.top-conter {
					display: flex;
					align-items: center;
					border-bottom: 1px solid #7a7a7a;

					.conter-left {
						font-size: 18px;
						margin: 0 10px;
						color: red;
					}

					.conter-right {
						width: 100%;
					}

				}

				.top-top {
					height: calc(100% - 32px);
					overflow-y: scroll;

					.top-title {
						font-size: 16px;
						padding: 5px 5px;
						display: flex;
						justify-content: space-between;
						align-items: center;
						text-align: center;
					}

					.top-title1 {
						font-size: 16px;
						padding: 0 5px 5px;
						display: flex;
						justify-content: space-between;
						align-items: center;
					}

					.xian {
						width: 100%;
						height: 2rpx;
						background-color: #7a7a7a;
					}
				}
			}

		}

		.btn {
			display: flex;
			align-items: center;
			padding: 10px 5px;
			background-color: #ededed;
		}
	}
</style>
